package com.zkingsoft.codeApi;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.generator.NameConvert;
import com.zkingsoft.generator.impl.DefaultNameConvert;
import com.zkingsoft.generator.impl.DefaultTypeHandle;

/**
 * 数据表和java的对应模型
 * 
 * @author Matrix-J
 *
 */
public class TableClassModel{

	/**
	 * 导入类列表
	 */
	private Map<String, String> importList;
	/**
	 * 包名
	 */
	private String packageName;

	/**
	 * 类名
	 */
	private String className;
	
	/**
	 * 表名
	 */
	private String tableName;
	
	/**
	 * 类的变量名称
	 */
	private String classVariableName;
	/**
	 * 主键
	 */
	private PropertyColumn primaryKey;
	
	
	
	private List<PropertyColumn> mapping;
	

	/**
	 * 根据[javaBean]构造一个数据表模型
	 * 
	 * @param classs
	 * @param format
	 */
	public TableClassModel(Class classs, NameConvert format) {

		if (format == null) {
			format = new DefaultNameConvert();
		}
		this.className = classs.getSimpleName();
		this.tableName = format.classNameToTableName(classs.getSimpleName());
		Field[] fields = classs.getDeclaredFields();
		this.mapping = new ArrayList<PropertyColumn>();
		for (Field f : fields) {
			// 排除复杂类型数据
			if (isPrimitive(f.getType())) {
				PropertyColumn p = new PropertyColumn();
				String propertyName = f.getName();
				p.setProperty(propertyName);
				p.setColumn(format.propertyToColumn(propertyName));
				this.mapping.add(p);
			}
		}
	}

	public TableClassModel(Configuration configuration, String tableName, List<Map> rows, NameConvert format) {
		this.importList=new HashMap<String, String>();
		if (format == null) {
			format = new DefaultNameConvert();
		}
		this.importList.put("Serializable", "java.io.Serializable");
		this.tableName = tableName;
		this.className = format.tableNameToClassName(tableName);
		this.classVariableName =format.classNameToVariableName(this.className);
		this.mapping = new ArrayList<PropertyColumn>();
		for (Map row : rows) {
			PropertyColumn p = new PropertyColumn();
			String columnName = (String) row.get("_column");
			p.setColumn(columnName);
			p.setProperty(format.columnToProperty(columnName));
			p.setJdbcType((String) row.get("jdbcType"));
			BaseController.log.info("jdbcType="+p.getJdbcType());
			p.setClassType(getClassTypeByJdbcType(p.getJdbcType()));
			if(p.getClassType().equals("Date")){
				this.importList.put("date", "java.util.Date");
			}else if(p.getClassType().equals("BigDecimal")){
				this.importList.put("date", "java.math.BigDecimal");
			}
			p.setMemo((String) row.get("memo"));
			p.setMethodName(format.propertyToMethod(p.getProperty()));
			Object isPrimaryKey = row.get("isPrimaryKey");
			// 这里没有考虑多数据库的情况
			if (isPrimaryKey != null && isPrimaryKey.toString().equals("PRI")) {
				p.setIsPrimaryKey(true);
				this.primaryKey=p;
			}else{
				p.setIsPrimaryKey(false);
			}
			
			this.mapping.add(p);
		}
	}

	/**
	 * 根据jdbctype获取类类型
	 * 
	 * @param jdbcType
	 * @return
	 */
	private String getClassTypeByJdbcType(String jdbcType) {
		return DefaultTypeHandle.TypeMapping.get(DataBaseType.MYSQL).get(jdbcType);
	}

	/**
	 * 判断是否为复杂类型
	 * 
	 * @param classs
	 * @return
	 */
	private boolean isPrimitive(Class classs) {
		if (classs.isPrimitive()) {
			return true;
		} else {
			if (classs == Double.class)
				return true;
			else if (classs == Integer.class)
				return true;
			else if (classs == Long.class)
				return true;
			else if (classs == Boolean.class)
				return true;
			else if (classs == Float.class)
				return true;
			else if (classs == Byte.class)
				return true;
			else if (classs == Character.class)
				return true;
			else if (classs == Short.class)
				return true;
			else if (classs == String.class)
				return true;
			else if (classs == Date.class)
				return true;
		}
		return false;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<PropertyColumn> getMapping() {
		return mapping;
	}

	public void setMapping(List<PropertyColumn> mapping) {
		this.mapping = mapping;
	}


	public PropertyColumn getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(PropertyColumn primaryKey) {
		this.primaryKey = primaryKey;
	}

	

	public Map<String, String> getImportList() {
		return importList;
	}

	public void setImportList(Map<String, String> importList) {
		this.importList = importList;
	}

	public String getClassVariableName() {
		return classVariableName;
	}

	public void setClassVariableName(String classVariableName) {
		this.classVariableName = classVariableName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	
	
}
